<?php 
ob_start();
session_start();
$teamid=$_SESSION['teamID'];
if ($_SESSION['teamID']!=1){header('LOCATION:main.php');}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Team</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/bootstrap-datepicker3.css" rel="stylesheet" />
<link href="Content/site.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/bootstrap.js"></script>
<script src="Scripts/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="js/form_validation.js"></script>
<script> 
	function showAssigntable(id){
		
		var element="assigntable"+id;
		var button="assign"+id;
		if (document.getElementById(element).style.display=="none"){
			
			document.getElementById(element).style.display="";
			document.getElementById(button).innerHTML="Hide";
		}
		else{
			document.getElementById(element).style.display="none";
			document.getElementById(button).innerHTML="Assign Player";
		}
	}
</script>
</head>
<body>
<div class="container blue_border">
<?php include_once("php/team_classes.php");?>
<?php include_once("php/banner.php");?>
<?php include_once("php/nav_bar.php");?>
  <h3 class="align-center">Add New Team</h3>
  <div class="container-fluid">
    <form class="form" action="php/addteam.php" method="post" name="addPlayer" >
    	<table class="table table-bordered">
        	<tr>
            	<td>Team Name</td><td>Division</td><td>Password</td><td></td>
            </tr>
            <tr>
            	<td><input type="text" class="form-control" name="team_name"  id="team_name" /></td>
                <td><?php $divisions=$repDivision->maps($repDivision->allDivision()); ?> 
                <select name="division" id="division" class="form-control"> 
                <option> </option>
                <?php foreach ($divisions as $d) {?>
                <option value="<?php echo $d->divisionID;?>"> <?php echo $d->division_name;?> </option>              
                <?php }?>
                </select>
                </td>
                <td><input type="text" class="form-control" name="password"  id="password" /></td>
                <td><button class="btn btn-default btn-md" type="submit" name="submit" onClick="return validateTeamAdd();" >Add</button></td>
            </tr>
        </table>
    </form>
  </div>
  <div class="container-fluid">
   <h3 class="align-center">Current Teams</h3>
  	
  	
     	<table class="table table-striped table-bordered" >
        	<thead>
            <tr>
                <th>Team Name</th><th>Division</th><th>Password</th><th>Status</th><th></th>
            </tr>  
            </thead>
            <tbody>
            <?php foreach ($divisions as $d) {?> 
            <?php $teams=$repTeam->maps($repTeam->getActiveTeams($d->divisionID));?>
        <?php foreach ($teams as $t){?>
            <tr>
            <form class="form" action="php/updateteam.php" method="post" >
            <input type="hidden" class="form-control" name="teamID"  id="teamID" value="<?php echo $t->teamID; ?>" />    
            <td><input type="text" class="form-control" name="team_name"  id="team_name" value="<?php echo $t->team_name; ?>" /></td>
            <td> <select name="division" id="division" class="form-control"> 
            	<option value="<?php echo $d->divisionID;?>"> <?php echo $d->division_name;?> </option>
                <?php foreach ($divisions as $div) {?>
                <option value="<?php echo $div->divisionID;?>"> <?php echo $div->division_name;?> </option>              
                <?php }?>
                </select></td>
            <td><input type="text" class="form-control" name="password"  id="password" /></td>
            <td><label class="checkbox-inline"><input type="radio" name="team_status<?php echo $t->teamID; ?>" id="active" value="Active" <?php if ($t->team_status=="Active"){echo 'checked';} ?>/> Active</label>
            	<label class="checkbox-inline"><input type="radio" name="team_status<?php echo $t->teamID; ?>" id="disabled" value="Disabled" <?php if ($t->team_status=='Disabled'){echo 'checked';} ?> /> Disable</label></td>
            <td><button class="btn btn-default btn-md" type="submit" name="submit">Update</button> <button class="btn btn-default btn-md" type="button" name="assign" id="assign<?php echo $t->teamID; ?>" onClick="showAssigntable(<?php echo $t->teamID; ?>)">Assign Players</button></td>
            </form>
            </tr>
            <tr id="assigntable<?php echo $t->teamID; ?>" style="display:none;">
            	<td colspan="5">
                <table class="table table-bordered">
                <thead>
                <tr>
                    <th>Player 1</th>
                    <th>Player 2</th>
                    <th>Player 3</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                <form class="form" action="php/assignplayers.php" method="post" >
                    <input type="hidden" class="form-control" name="teamID"  id="teamID" value="<?php echo $t->teamID; ?>" /> 
                    <?php $players=$repPlayer->maps($repPlayer->getAvailablePlayersForTeam());
                    $teamPlayers=$repTeam->getPlayerByTeamID($t->teamID);
                    ?>
                    <td> <select name="player1" id="player1" class="form-control"> 
                    <option value="<?php echo $teamPlayers[0]; ?>"> <?php $player1=$repPlayer->map($repPlayer->readPlayer($teamPlayers[0])); echo $player1->fname." ".$player1->sname;?> </option>
                    <?php foreach ($players as $p) {?>
                    <option value="<?php echo $p->playerID;?>"> <?php echo $p->fname." ".$p->sname;?> </option>              
                    <?php }?>
                    </select></td> 
                    <td> <select name="player2" id="player2" class="form-control"> 
                    <option value="<?php echo $teamPlayers[1]; ?>"> <?php $player2=$repPlayer->map($repPlayer->readPlayer($teamPlayers[1])); echo $player2->fname." ".$player2->sname;?> </option>
                    <?php foreach ($players as $p) {?>
                    <option value="<?php echo $p->playerID;?>"> <?php echo $p->fname." ".$p->sname;?> </option>              
                    <?php }?>
                    </select></td>
                    <td> <select name="player3" id="player3" class="form-control"> 
                    <option value="<?php echo $teamPlayers[2]; ?>"> <?php $player3=$repPlayer->map($repPlayer->readPlayer($teamPlayers[2])); echo $player3->fname." ".$player3->sname;?> </option>
                    <?php foreach ($players as $p) {?>
                    <option value="<?php echo $p->playerID;?>"> <?php echo $p->fname." ".$p->sname;?> </option>              
                    <?php }?>
                    </select></td>
                    <td><button class="btn btn-default btn-md" type="submit" name="submit">Assign</button></td>
                </form>
                </tbody>
                </table>
                </td>
            </tr>
      	<?php }}?>
        </tbody>
  	</table>

  </div>
</div>
<?php ob_flush();?>
</body>
</html>
